﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using Fuse8.DomainFramework.Common.Locator;
using Fuse8.DomainFramework.DataAccess;
using Fuse8.DomainFramework.DataAccess.SqlDatabase;
using Fuse8.GlobalizationFramework.Domain;
using Fuse8.GlobalizationFramework.Domain.Commands;
using Fuse8.GlobalizationFramework.Infrastructure;

namespace Fuse8.GlobalizationFramework.DataAccess.SqlRepository
{
    internal class NativeDataAccessPolicy
    {
        public static void Init()
        {
            DependencyServiceLocator.Current.SetDefaultService<IResourceCommandFactory>(new ResourceCommandFactory());

            var unitOfWork =
                SqlEntityDataRepositoryConfiguration<Resource, int>.CreateUnitOfWorkFactory(ConnectionString);
            DependencyServiceLocator.Current.SetDefaultService(unitOfWork);

            var mapper =
                SqlEntityDataRepositoryConfiguration<Resource, int>.CreateMapper(new List<string>() {"Translated", "PublicGroupId"});
            DependencyServiceLocator.Current.SetDefaultService(mapper);
            DependencyServiceLocator.Current.SetDefaultService<IEntityDataRepository<Resource, int>>(new ResourceSqlRepository());
        }

        protected static string ConnectionString
        {
            get
            {
                string name = DependencyServiceLocator.Current.GetInstance<IGlobalizationSetting>().ConnectionStringName;
                return ConfigurationManager.ConnectionStrings[name].ConnectionString;
            }
        }
    }
}